using System;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.Collections;
using System.Xml.Schema;
using System.Xml;

namespace SystemX.Infrastructure
{
  /// <summary>
  /// xml Serializable String Dictionary.
  /// For a serializable Dictionary,see to SerializableDictionary
  /// </summary>
  [CLSCompliant(true)]
  [Serializable()]
  public class SerializableStringDictionary : StringDictionary, IXmlSerializable
  {
    #region IXmlSerializable Members

    /// <summary>
    /// Get Schema
    /// </summary>
    /// <returns></returns>
	  public XmlSchema GetSchema()
	  {
		  return null;
	  }
    /// <summary>
    /// Generates a collection from its XML representation.
    /// </summary>
    /// <param name="reader">System.Xml.XmlReader</param>
	  public void ReadXml(XmlReader reader)
	  {
		  this.Clear();
		  if (reader.ReadToDescendant("SerializableStringDictionary"))
		  {
			  if (reader.ReadToDescendant("DictionaryEntry"))
			  {
				  do
				  {
					  reader.MoveToAttribute("Key");
					  string key = reader.ReadContentAsString();
					  reader.MoveToAttribute("Value");
					  string value = reader.ReadContentAsString();

					  this.Add(key, value);

				  } while (reader.ReadToNextSibling("DictionaryEntry"));
			  }
		  }
	  }
    /// <summary>
    /// Loads collection to XML writer
    /// </summary>
    /// <param name="writer">System.Xml.XmlWriter</param>
	  public void WriteXml(XmlWriter writer)
	  {
		  writer.WriteStartElement("SerializableStringDictionary");
		  foreach (DictionaryEntry entry in this)
		  {
			  writer.WriteStartElement("DictionaryEntry");
			  writer.WriteAttributeString("Key", (string)entry.Key);
			  writer.WriteAttributeString("Value", (string)entry.Value);
			  writer.WriteEndElement();
		  }
		  writer.WriteEndElement();
	  }

    #endregion
  }
}